package yxy.cherry.paycenter.channelmodule.sdk_xiaomi_quick;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import yxy.apple.logger.Logger;
import yxy.apple.util.HmacSHA1Encryption;
import yxy.apple.util.MD5Utility;
import yxy.apple.util.SdkUtil;
import yxy.cherry.data.bean.charge;
import yxy.cherry.paycenter.channelmodule.PayMoudle_channel;
import yxy.cherry.paycenter.database.db.orders;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.channel.SdkChannel;
import yxy.game.pm2.bean.db.sdk.SDK_UnionChannel;
import yxy.game.pm2.bean.db.sdk.SDK_UnionChannelDAO;
import yxy.game.pm2.bean.module.OrderInfoModule;
import yxy.game.pm2.bean.order.OrderInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

//客户端下单（创建订单）
public class PayMoudle_xiaomi_quick extends PayMoudle_channel {
	static final Logger logger = Logger.create(PayMoudle_xiaomi_quick.class);

	@Override
	public String getChannel() {
		return SdkChannel.sdk_xiaomi_quick;
	}
	@Override
	public JSONObject dealPay(JSONObject infoobj) {
		// TODO Auto-generated method stub
		return null;
	}

	//构造订单信息，生成签名，返回给客户端
	@Override
	public JSONObject createOrderInfo(JSONObject infoobj, orders o) throws Exception{
		JSONObject extObj = JSONObject.parseObject(infoobj.getString("ext")); //扩展参数
		String openId = extObj.getString("openId");
		String appId = extObj.getString("appId");
		String session = extObj.getString("session");

		//读取渠道配置
		SDK_UnionChannel unionChannel = SDK_UnionChannelDAO.getByAppId(o.getChannel(), appId);
		//生成sign
		Map<String, String> params = new HashMap<>();
		params.put("appId", appId);
		params.put("appAccountId", openId);
		params.put("session", session);
		params.put("cpOrderId", o.getMyorderid());
		params.put("cpUserInfo", appId); //cp透传信息 (非空)
		params.put("displayName", o.getGoodsname());
		params.put("feeValue", String.valueOf(o.getGoodsprice())); //分
		logger.info(SdkChannel.sdk_xiaomi_quick + ":createOrderInfo : %s", JSON.toJSON(params));
		String content = SdkUtil.mapToContent(params, false);
		String sign = HmacSHA1Encryption.HmacSHA1Encrypt(content, unionChannel.getAppSecret());
		logger.info(SdkChannel.sdk_xiaomi_quick + ":createOrderInfo:content:%s:$s", content, sign);
		params.put("sign", sign);

		//返回订单信息
		JSONObject json = new JSONObject();
		json.put("orderInfo", params);
		logger.info(SdkChannel.sdk_xiaomi_quick + ":createOrderInfo : %s", JSON.toJSON(json));
		return json;
	}

	//创建订单
	public orders createOrderXIAOMIQUICK(int dataid, String playercode, int serverid, int acttype) {
		orders o = createOrder(getChannel(), dataid, acttype, playercode, serverid);
		addOrder(o);
		return o;
	}
	
	/**
	 * 创建 OrderInfo
	 * @param o
	 * @param curs
	 * @param c
	 * @param payamount
	 */
	public void createOrderInfoModule(orders o, ArrayList<Currency> curs, charge c, int payamount) {
		createOrderInfoModule(o, curs, c.getVipexp(), payamount);
	}
	
	/**
	 * 
	 * @param o
	 * @param curs
	 * @param vipexp
	 * @return
	 */
	public void createOrderInfoModule(orders o, ArrayList<Currency> curs, int vipexp, int payamount) {
		OrderInfo oi = OrderInfoModule.createOrderInfo(o.getGoodsid(), o.getChannel(), o.getMyorderid(), new DateTime(o.getCreatetime().getTime()), 
				OrderInfoModule.orderinforuning, o.getPlatorderid(), o.getActtype(), o.getPlayercode(), curs, 
				o.getServerid(), DateTime.now(), vipexp, payamount, o.getGoodsprice());
		logger.info("createOrderInfoModule %s", JSON.toJSONString(oi));
		OrderInfo.set(o.getPlayercode(), oi);
	}
}
